package cn.edu.nju.ws.sview.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.edu.nju.ws.sview.rdf.ResourceFactory;
import cn.edu.nju.ws.sview.views.Entity;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

/**
 * @author Gong Saisai
 */
public class URIUtil {
	/**
	 * 
	 * @param itemType_int : 11="uri dataset",12="custom dataset",2="uri entity",1="custom entity",3="collection"
	 * @return type_str:uri="uri dataset",custom="custom dataset",o="uri entity",e="custom entity",c="collection"
	 */
	public static String getItemTypeStr(int itemType_int){
		String type_str="";
		if(itemType_int == DatasetItem.URI_INT){
			type_str = DatasetItem.URI_GENRE;
		}else if(itemType_int == DatasetItem.CUSTOM_INT){
			type_str = DatasetItem.CUSTOM_GENRE;
		}else if(itemType_int == Entity.URI_ENTITY){
			type_str = "o";
		}else if(itemType_int == Entity.CUSTOM_ENTITY){
			type_str = "e";
		}else if(itemType_int == Entity.COLLECTION){
			type_str = "c";
		}
		return type_str;
	}
	/**
	 * 
	 * @param itemType_str:uri="uri dataset",custom="custom dataset",o="uri entity",e="custom entity",c="collection"
	 * @return type_int : 11="uri dataset",12="custom dataset",2="uri entity",1="custom entity",3="collection"
	 */
	public static int getItemTypeInt(String itemType_str){
		int type_int=-1;
		if(itemType_str.contentEquals(DatasetItem.URI_GENRE)){
			type_int = DatasetItem.URI_INT;
		}else if(itemType_str.contentEquals(DatasetItem.CUSTOM_GENRE)){
			type_int = DatasetItem.CUSTOM_INT;
		}else if(itemType_str.contentEquals("o")){
			type_int = Entity.URI_ENTITY;
		}else if(itemType_str.contentEquals("e")){
			type_int = Entity.CUSTOM_ENTITY;
		}else if(itemType_str.contentEquals("c")){
			type_int = Entity.COLLECTION;
		}
		return type_int;
	}
    /**
     * get the uri local name
     * @param uri
     * @return
     */
	public static String getLocalname(String uri){
		String localname = null;
		int posHash = uri.lastIndexOf('#');
		if(posHash!=-1){
			localname = uri.substring(posHash+1);
		}else{
			while(uri.lastIndexOf("/")!=-1&&uri.substring(uri.lastIndexOf("/")+1).equals(""))
				uri = uri.substring(0,uri.length()-1);
			if(uri.lastIndexOf("/")!=-1)
				localname = uri.substring(uri.lastIndexOf("/")+1);
			else if(uri.lastIndexOf(":")!=-1)
				localname = uri.substring(uri.lastIndexOf(":")+1);
			else 
				localname = uri;
		}
		return localname;
	}
	
	public static String removeParams(String url, String[] params) { 
		String reg = null; 
		StringBuffer ps = new StringBuffer();
		ps.append("("); 
		for(int i = 0; i < params.length; i++) { 
			ps.append(params[i]).append("|"); 
		}
		ps.deleteCharAt(ps.length() - 1); 
		ps.append(")"); 
		reg = "(?<=[\\?&])" + ps.toString() + "=[^&]*&?"; 
		url = url.replaceAll(reg, "");
		url = url.replaceAll("(\\?|&+)$", "");
		return url; 
	} 
	
	public static String removeParam(String url, String param) { 
		String reg = null; 
		StringBuffer ps = new StringBuffer();
		ps.append("("); 
			ps.append(param); 
		ps.append(")"); 
		reg = "(?<=[\\?&])" + ps.toString() + "=[^&]*&?"; 
		url = url.replaceAll(reg, "");
		url = url.replaceAll("(\\?|&+)$", "");
		return url; 
	} 
	
	/**
	 * get the correspond value of specif param in uri
	 * @param url
	 * @param param
	 * @return
	 */
	public static String testParam(String url, String param) { 
		String result  = null;
		String reg = null; 
		StringBuffer ps = new StringBuffer();
		ps.append("("); 
			ps.append(param); 
		ps.append(")"); 
		reg = "(?<=[\\?&])" + ps.toString() + "=[^&]*&?"; 
		Pattern pattern  = Pattern.compile(reg);
		Matcher mc = pattern.matcher(url);
		while(mc.find()){
			result = mc.group().split("=")[1] ;
		}
		return result; 
	} 
	
	/**
	 * parse custom datamember uri and return author and name
	 * datamember_uri_prefix+group/isInner/classID/isCopy/name
	 * @param uri. the custom datamember uri
	 * @return first group, second isInner(true,false), third classID,fourth isCopy,final name
	 */
	public static ArrayList<String> parseCustomDatamemberURI(String uri){
		ArrayList<String> result = new ArrayList<String>(2);
		int start = ResourceFactory.CUSTOM_DATAMEMBER_URI_PREFIX.length();
		String tmp = uri.substring(start) ;
		int firstSlash = tmp.indexOf("/");
		String group = tmp.substring(0,firstSlash) ;
		
		String isInnerString = tmp.substring(group.length()+1) ;
		firstSlash= isInnerString.indexOf("/") ;
		String isInner =isInnerString.substring(0,firstSlash) ;
		
		String classIDString = isInnerString.substring(isInner.length()+1) ;
		firstSlash= classIDString.indexOf("/") ;
		String classID =classIDString.substring(0,firstSlash) ;
		
		String isCopyString = classIDString.substring(classID.length()+1) ;
		firstSlash= isCopyString.indexOf("/") ;
		String isCopy =isCopyString.substring(0,firstSlash) ;
		
	    int lastSlash =  tmp.lastIndexOf("/") ;
		String  name= tmp.substring(lastSlash+1) ;
		result.add(group) ;
		result.add(isInner) ;
		result.add(classID) ;
		result.add(isCopy) ;
		result.add(name) ;
		return result;
	}
	
	public static void main(String[] args) {
	}
}	
